package com.hc.controller;

import com.hc.bean.User;
import com.hc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("哈哈哈")
@RestController
@RequestMapping("/user")
public class TestController {

    /*测试Redis*/
    @Resource(name = "userServiceImpl1")
    private UserService userService;

    @ApiOperation("通过id查询用户")
    @RequestMapping("/findUserById")
    public Map<String,Object> getById(Integer id) {
        Map<String,Object> resultMap=new HashMap<>();
        User user=userService.getUserById(id);
        if(user!=null){
            resultMap.put("success",true);
            resultMap.put("msg","查询成功");
            resultMap.put("results",user);
        }else {
            resultMap.put("success",false);
            resultMap.put("msg","该用户不存在");
        }
        return resultMap;
    }

    @ApiOperation("通过id查询用户")
    @RequestMapping("/updateUser")
    /*
    * 前端发过来的数据是JSON数字:Content-Type: application/json;charset=UTF-8
    * 所以这里要加@RequestBody
    * */
    public Map<String,Object> update(@RequestBody User user) {
        System.out.println(user);
        Map<String,Object> resultMap=new HashMap<>();
        if(userService.updateUser(user)>0){
            resultMap.put("success",true);
            resultMap.put("msg","修改成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("msg","该用户不存在");
        }
        return resultMap;
    }

    @RequestMapping("/findAll")
    public Map<String,Object> findAll(){
        Map<String,Object> resultMap=new HashMap<>();
        List<User> userList=userService.findAll();
        if(userList.size()>0){
            resultMap.put("success",true);
            resultMap.put("msg","查询成功");
            resultMap.put("results",userList);
        }else {
            resultMap.put("success",false);
            resultMap.put("msg","数据为空");
        }
        return resultMap;
    }

    @RequestMapping("/deleteUser")
    public Map<String,Object> delete(Integer id){
        Map<String,Object> resultMap=new HashMap<>();
        if(userService.deleteUser(id)>0){
            resultMap.put("success",true);
            resultMap.put("msg","删除成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("msg","删除失败");
        }
        return resultMap;
    }

    @RequestMapping("/addUser")
    public Map<String,Object> addUser(@RequestBody User user){
        Map<String,Object> resultMap=new HashMap<>();
        if(userService.addUser(user)>0){
            resultMap.put("success",true);
            resultMap.put("msg","用户添加成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("msg","用户添加失败");
        }
        return resultMap;
    }
}
